﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;

namespace dnsServerCSharp
{
    public class FileHelpaer
    {
        public static bool fileExist(string path)
        {
            try
            {
                return new System.IO.FileInfo(path).Exists;
            }
            catch (System.Exception)
            {
                return false;
            }
        }
    }
    public class ARecordConfig
    {
        public static void loadInto(ARecordTable aRecordTable)
        {
            if (FileHelpaer.fileExist(aRecordTableConfigFileName))
            {
                var reader = new StreamReader(aRecordTableConfigFileName);
                while (reader.Peek() != -1)
                {
                    var line = reader.ReadLine();
                    if (line[0] != '#')
                    {
                        var ss = line.Split(seprator);
                        if (ss.Length >= 2)
                            aRecordTable.insert(ss[0], ss[1]);
                    }
                }
                reader.Close();
            }
            return;
        }
        static string aRecordTableConfigFileName = "aRecordTable.txt";
        static char[] seprator = {' '};
    }
    public class FilterConfig
    {
        public static void loadInto(Filter filter)
        {
            if (FileHelpaer.fileExist(filterConfigFileName))
            {
                var reader = new StreamReader(filterConfigFileName);
                while (reader.Peek() != -1)
                {
                    var line = reader.ReadLine();
                    if (line.Length > 0 && line[0] != '#')
                        filter.insert(line);
                }
                reader.Close();
            }
            return;
        }
        static string filterConfigFileName = "filter.txt";
    }

    public class ServerConfig
    {
        static string serverConfigFileName = "serverConfig.txt";
        public static ServerConfig loadInto(Server server)
        {
            ServerConfig serverConfig = new ServerConfig();
            if (FileHelpaer.fileExist(serverConfigFileName))
            {
                /*
redirectServerName=192.168.1.1
                 * */
                var reader = new StreamReader(serverConfigFileName);
                Regex ex = new Regex("^([^=]+)=(.*)$");
                Dictionary<string, string> args = new Dictionary<string, string>();
                while (reader.Peek() != -1)
                {
                    var line = reader.ReadLine();
                    if (line[0] != '#')
                    {
                        var match = ex.Match(line);
                        if (match.Success)
                        {
                            args.Add(match.Groups[1].Value, match.Groups[2].Value);
                        }
                    }
                }

                server.redirectServerName = Convert.ToString(args["redirectServerName"]);
                server.listeningPort = Convert.ToInt32(args["listeningPort"]);
                FilterConfig.loadInto(server.filter);
                ARecordConfig.loadInto(server.aRecordTable);
                reader.Close();
            }
            return serverConfig;
        }
    }
    public class Config
    {
        public static void loadInto(Server s)
        {
            ServerConfig.loadInto(s);
            return;
        }
    }
}
